import com.du.spring.pojo.User;
import com.du.spring.service.UserService;
import com.du.spring.service.impl.UserServiceImpl;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : DuJiabao
 * @Date : 2021/2/5 10:35
 * @Project : spring
 * @File : null.java
 * @Desc :
 */
public class Demo {
  private static final Logger logger = Logger.getLogger(Demo.class);
  ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
  UserService userServiceImpl = context.getBean("userServiceImpl", UserServiceImpl.class);

  @Test
  public void addUserTest() {
    int res = userServiceImpl.addUser(new User("jhon", "1434314"));
    if (res > 0)
      logger.info("添加成功");
    else
      logger.info("添加失败");
  }

  @Test
  public void countUserTest() {
    int res = userServiceImpl.countUser();
    logger.info("共有" + res + "个User");
  }

  @Test
  public void selectUserByIdTest() {
    User user = userServiceImpl.selectUserById(9);
    logger.info(user);
  }

  @Test
  public void selectUsersTest() {
    List<User> users = userServiceImpl.selectUsers();
    logger.info(users);
  }

  @Test
  public void deleteUserByIdTest() {
    int res = userServiceImpl.deleteUserById(2);
    if (res > 0)
      logger.info("删除成功");
    else
      logger.info("删除失败");
  }

  @Test
  public void updateUserTest() {
    int res = userServiceImpl.updateUser(new User(1, "nihao", "413413"));
    if (res > 0)
      logger.info("更新成功");
    else
      logger.info("更新失败");
  }

  @Test
  public void batchAddUsersTest() {
    List<Object[]> batchArgs = new ArrayList<>();
    Object[] o1 = {"nihao", "458785"};
    Object[] o2 = {"shijie", "458785"};
    batchArgs.add(o1);
    batchArgs.add(o2);
    userServiceImpl.batchAddUsers(batchArgs);
  }

  @Test
  public void batchUpdateUsersTest() {
    List<Object[]> batchArgs = new ArrayList<>();
    Object[] o1 = {"nihao", "458785", 5};
    Object[] o2 = {"shijie", "458785", 6};
    batchArgs.add(o1);
    batchArgs.add(o2);
    userServiceImpl.batchUpdateUsers(batchArgs);
  }

  @Test
  public void batchDeleteUsersTest() {
    List<Object[]> batchArgs = new ArrayList<>();
    Object[] o1 = {5};
    Object[] o2 = {6};
    batchArgs.add(o1);
    batchArgs.add(o2);
    userServiceImpl.batchDeleteUsers(batchArgs);
  }
}
